<template>
  <div style="padding-top: 20px">
    <!--    搜索区域-->
    <div style="margin: 10px 0">
      <el-input @keyup.enter.native="inquire" v-model="search" placeholder="请输入关键字查询" style="width: 40%;background-color: #fff">
        <el-select slot="prepend" v-model="select" placeholder="请选择" style="width: 120px">
          <el-option label="会员名称" value="memberName" />
          <el-option label="手机号" value="phoneNumber" />
        </el-select>
        <el-button slot="append" icon="el-icon-search" @click="inquire" />
      </el-input>
    </div>
    <!--    展示选中会员信息-->
    <el-table
      v-show="memberShowData.length"
      ref="multipleTable"
      :data="memberShowData"
      tooltip-effect="dark"
      style="width: 100%"
      border
      stripe
      :header-cell-style="headClass"
      :cell-style="rowClass"
    >
      <el-table-column
        prop="memberName"
        label="会员名称"
      />
      <el-table-column
        prop="phoneNumber"
        label="手机号"
      />
      <el-table-column
        prop="birthday"
        label="生日"
        :formatter="formatDate"
      />
      <el-table-column
        prop="balance"
        label="余额"
      />
      <el-table-column
        prop="remark"
        label="备注"
      />
      <el-table-column label="操作" width="140">
        <template #default="scope">
          <el-button type="primary" size="mini" @click="handlePay(scope.row)">购买</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--    选择会员弹窗-->
    <el-dialog
      title="请选择会员"
      :visible.sync="selectRechargeVisible"
      width="80%"
      center
      :show-close="false"
    >
      <!--    会员表格区域-->
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        border
        stripe
        :header-cell-style="headClass"
        :cell-style="rowClass"
      >
        <el-table-column
          label="序号"
          type="index"
          :index="indexMethod"
          sortable
        />
        <el-table-column
          prop="memberName"
          label="会员名称"
        />
        <el-table-column
          prop="phoneNumber"
          label="手机号"
        />
        <el-table-column
          prop="birthday"
          label="生日"
          :formatter="formatDate"
          sortable
        />
        <el-table-column
          prop="balance"
          label="余额"
          sortable
        />
        <el-table-column
          prop="remark"
          label="备注"
        />
        <el-table-column label="操作" width="140">
          <template #default="scope">
            <el-button type="primary" size="mini" @click="handleSelect(scope.row)">选择</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--    会员分页-->
      <div style="margin: 10px 0">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-dialog>
    <!--    支付弹窗-->
    <PlayVisible
      ref="playVisible"
      :member="member"
    />
  </div>
</template>

<script>

import { selectMember } from '@/api/registerMember'
import PlayVisible from '@/components/PlayVisible'
import { EventBus } from '@/event-bus'
import { selectMemberById } from '@/api/recharge'
export default {
  name: 'BasePay',
  components: {
    PlayVisible
  },
  data() {
    return {
      // 会员数据
      member: {},
      // 选择会员弹窗
      selectRechargeVisible: false,
      // 会员信息展示表
      memberShowData: [],
      // 每页查看数量
      pageSizes: [10, 20, 50, 200],
      // 每页显示数量
      pageSize: 10,
      // 总数量
      total: 0,
      // 当前页
      currentPage: 1,
      // 查询输入框
      search: '',
      // 查询选择项目
      select: 'memberName',
      // 表格数据
      tableData: []
    }
  },
  methods: {
    // 选择会员弹窗,选中按钮
    handleSelect(row) {
      // 打开新增窗口,改变为编辑
      this.selectRechargeVisible = false
      EventBus.$emit('memberData', row)
      // 赋值查看
      this.memberShowData = [row]
      this.$refs.playVisible.gooseData = []
    },
    // 刷新member 工具
    flashMember() {
      new Promise((resolve, reject) => {
        selectMemberById(this.member).then(res => {
          this.member = res.data
          this.memberShowData = []
          this.memberShowData = [res.data]
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 购买
    handlePay(row) {
      // 查询请求,刷新member数据
      this.member = row
      this.flashMember()
      this.$refs.playVisible.addPayVisible = true
    },
    // 页码跟序号处理 工具
    indexMethod(index) {
      const currentPage = this.currentPage // 当前页码
      const pageSize = this.pageSize // 每页条数
      return index + 1 + (currentPage - 1) * pageSize // 返回表格序号
    },
    // 日期处理 工具
    formatDate(row, column) {
      // 获取单元格数据
      const data = row[column.property]
      if (data == null) {
        return null
      }
      const dt = new Date(data)
      return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate()
    },
    // 点击查询
    inquire() {
      // 调用查询方法
      this.selectPage(this.currentPage, this.pageSize)
      // 打开弹窗
      this.selectRechargeVisible = true
    },
    // 查询代码抽取 工具
    selectPage(currentPage, pageSize) {
      new Promise((resolve, reject) => {
        // 查询请求
        selectMember(currentPage, pageSize, this.search, this.select).then(res => {
          // 赋值给页面渲染
          this.tableData = res.data.records
          // 赋值总数
          this.total = res.data.total
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 改变每页显示条数触发
    handleSizeChange(val) {
      this.pageSize = val
      this.selectPage(this.currentPage, val)
    },
    // 改变当前页码触发
    handleCurrentChange(val) {
      this.currentPage = val
      this.selectPage(val, this.pageSize)
    },
    // 表头居中显示
    headClass() {
      return 'text-align:center'
    },
    // 表格数据居中显示
    rowClass() {
      return 'text-align:center'
    }
  }
}
</script>

<style scoped>
</style>

